/**
 * @Author 韦建韩
 * @Date 2022/3/29
 */
package com.zczn.order_system.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.sql.Date;
import java.util.Objects;


@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object registerTime = this.getFieldValByName("registerTime", metaObject);
        if (Objects.isNull(registerTime))
            this.setFieldValByName("registerTime", new Date(System.currentTimeMillis()), metaObject);
        Object accounBalance = this.getFieldValByName("accounBalance", metaObject);
        if (Objects.isNull(accounBalance))
            this.setFieldValByName("accounBalance", BigDecimal.ZERO, metaObject);
        Object state = this.getFieldValByName("state", metaObject);
        if (Objects.isNull(state))
            this.setFieldValByName("state", "正常", metaObject);
        this.setFieldValByName("isDeleted", 0, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(System.currentTimeMillis()), metaObject);
    }
}
